package com.bupt.film.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bupt.enums.ResultCode;
import com.bupt.exceptions.BusinessException;
import com.bupt.film.entity.Film;
import com.bupt.film.entity.Hall;
import com.bupt.film.mapper.FilmMapper;
import com.bupt.film.mapper.HallMapper;
import com.bupt.film.service.FilmInfoService;
import com.bupt.order.client.OrderClient;
import com.bupt.order.entity.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class FilmInfoServiceImpl implements FilmInfoService {

    @Autowired
    private FilmMapper filmMapper;

    @Autowired
    private HallMapper hallMapper;

    @Autowired
    private OrderClient orderClient;

    @Override
    public List<Hall> getHallsInfoList() {
        return hallMapper.selectAll();
    }

    @Override
    public String getHallsInfo() {
        List<Hall> halls = hallMapper.selectAll();
        String s = JSON.toJSONString(halls);
        return "successCallBack("+s+")";
    }


    @Override
    public Hall getOneHallInfo(String hallId) {
        Hall hall = new Hall();
        hall.setHallId(hallId);
        return hallMapper.selectOne(hall);
    }

    @Override
    public List<Film> getFilmsInfo() {
        return filmMapper.selectAll();
    }

    @Override
    public Film getOneFilmInfo(String filmId) {
        Film film = new Film();
        film.setFilmId(filmId);
        return filmMapper.selectOne(film);
    }

    @Override
    public List<Film> getFilmsWithinTimeRange(String hallId, Date startTime, Date endTime) {
        //获取某个影厅下面所有的影片信息
        Hall certainHall = getOneHallInfo(hallId);
        String filmIdsStr = certainHall.getFilmIds();  //["film_1","film_3"]
        String[] filmIds = JSON.parseObject(filmIdsStr, String[].class);

        Example example = new Example(Film.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("filmId" , Arrays.asList(filmIds));
        List<Film> films = filmMapper.selectByExample(example);
//        System.out.println(films);

        //遍历判断电影是否在放映时间段内
        long startTimeLong = startTime.getTime();
        long endTimeLong = endTime.getTime();
        List<Film> inRangeFilms = new ArrayList<>();
        for (Film film : films) {
            if (startTimeLong <= film.getEndTime().getTime() && endTimeLong >= film.getStartTime().getTime()) {
                inRangeFilms.add(film);
            }
        }

        return inRangeFilms;
    }

    @Override
    public Map<String, Object> getFilmShowDetail(String hallId, String filmId, Date showDate) {

        //获取影厅和电影的基本信息
        Hall hallInfo = getOneHallInfo(hallId);
        Film filmInfo = getOneFilmInfo(filmId);

        //判断此电影是否在这个影厅中播放
        String filmIdsStr = hallInfo.getFilmIds();
        String[] filmIds = JSON.parseObject(filmIdsStr, String[].class);
        if (!Arrays.asList(filmIds).contains(filmInfo.getFilmId())) {
            return null;
        }

        //判断日期是否在电影上映时段内
        if (showDate.getTime() < filmInfo.getStartTime().getTime() || showDate.getTime() > filmInfo.getEndTime().getTime()) {
            return null;
        }

        //获取影厅的场次和价格
        String filmTimePeriodsStr = hallInfo.getFilmTimePeriods();
        String[] filmTimePeriods = JSON.parseObject(filmTimePeriodsStr, String[].class);
        Double ticketPrice = hallInfo.getTicketPrice();

        //结果封装json对象
        JSONObject resultObject = new JSONObject();
        resultObject.put("ticketPrice", ticketPrice);
        resultObject.put("allShowTime", filmTimePeriods);

        //每个场次的结果
        JSONArray eachShowArr = new JSONArray();

        //遍历所有场次，查询每个场次的票数
        for (String filmTimePeriod : filmTimePeriods) {
            // 获取某影厅某日期某场次的剩余票数
            // 可以根据hall_id, film_date, time_period查询订单表数量，然后做减法
            Order order = new Order();
            order.setHallId(hallId);
            order.setFilmDate(showDate);
            order.setTimePeriod(filmTimePeriod);
            //获取符合条件的订单数量
            Map<String, Object> ticketsSoldMap = orderClient.getTicketsSoldNumByModel(order);
            int ticketsSold = (int) ticketsSoldMap.get("ticketsSold");
            //获取本影厅的总座位数
            int seatNum = hallInfo.getSeatNum();
            //计算剩余座位数
            int remainingTickets = seatNum - ticketsSold;

            JSONObject oneShow = new JSONObject();
            oneShow.put("showTime", filmTimePeriod);
            oneShow.put("remainingTickets", remainingTickets);
            oneShow.put("ticketPrice", ticketPrice);
            eachShowArr.add(oneShow);
        }

        resultObject.put("ticketsCondition", eachShowArr);

        return resultObject;
    }

    public static void main(String[] args) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        Date date = new Date();
        System.out.println(date);

    }
}
